package librarianPanels;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.Font;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;

	import clientGUI.PanelDesign;
import clientGUI.MainGUI;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CreateReaderCardPanel extends PanelDesign {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textFieldID;
	private JTextField textFieldFirstName;
	private JTextField textFieldLastName;
	private JTextField textFieldPhone;
	private JTextField textFieldEMail;

	/**
	 * Create the panel.
	 */
	public CreateReaderCardPanel() {
		setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
		setLayout(null);
		setPreferredSize(new Dimension(644, 462));
		JLabel lblPleaseEnterDetails = new JLabel("Please Enter Details:");
		lblPleaseEnterDetails.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblPleaseEnterDetails.setBounds(238, 78, 180, 33);
		add(lblPleaseEnterDetails);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(209, 122, 46, 14);
		add(lblId);
		
		JLabel lblLastName = new JLabel("Last Name:");
		lblLastName.setBounds(209, 180, 79, 23);
		add(lblLastName);
		
		JLabel lblFirstName = new JLabel("First Name:");
		lblFirstName.setBounds(209, 152, 79, 14);
		add(lblFirstName);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(209, 212, 46, 14);
		add(lblPhone);
		
		JLabel lblEmail = new JLabel("E-Mail:");
		lblEmail.setBounds(209, 242, 46, 14);
		add(lblEmail);
		
		textFieldID = new JTextField();
		textFieldID.setText(null);
		textFieldID.setBounds(288, 120, 130, 20);
		add(textFieldID);
		textFieldID.setColumns(10);
		
		textFieldFirstName = new JTextField();
		textFieldFirstName.setText(null);
		textFieldFirstName.setBounds(288, 150, 130, 20);
		add(textFieldFirstName);
		textFieldFirstName.setColumns(10);
		
		textFieldLastName = new JTextField();
		textFieldLastName.setText(null);
		textFieldLastName.setBounds(288, 180, 130, 20);
		add(textFieldLastName);
		textFieldLastName.setColumns(10);
		
		textFieldPhone = new JTextField();
		textFieldPhone.setText(null);
		textFieldPhone.setBounds(288, 210, 130, 20);
		add(textFieldPhone);
		textFieldPhone.setColumns(10);
		
		textFieldEMail = new JTextField();
		textFieldEMail.setText(null);
		textFieldEMail.setBounds(288, 240, 130, 20);
		add(textFieldEMail);
		textFieldEMail.setColumns(10);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					int flag=1 ;
					String error = null ;
					String[] s=new String[6];
					s[0]="CreateNewReaderCard";
					s[1]=textFieldID.getText();  
					s[2]=textFieldFirstName.getText();
					s[3]=textFieldLastName.getText();
					s[4]=textFieldPhone.getText();
					s[5]=textFieldEMail.getText();
					if(MainGUI.isMsgEmpty(s))
					{
						flag=0;
						JOptionPane.showMessageDialog( new JFrame() , "You must fill all the fields");
					}
					else 
						{
							if(!MainGUI.isNumber(s[1]) || s[1].length()>10)   
							{
								error="Invalid ID";
								flag=0;
							}
							if(s[4].length()<9 || s[4].length()>10 ||  !MainGUI.isNumber(s[4]))
							{
								if(error==null) error= "Invalid number";
								else error+= ", Invalid Number ";
								flag=0;
							}
							if(!MainGUI.isName(s[2]) || !MainGUI.isName(s[3]))
							{
								if(error==null) error= "Invalid First or Last Name";
								else error+= ", Invalid First or Last Name ";
								flag=0;
							}
							if(!MainGUI.validateEmail(s[5]))
							{
								if(error==null) error="Invalid Email";
								else error+=" and Invalid Email";
								flag=0;
								if (flag==0) JOptionPane.showMessageDialog( new JFrame() , error);
							}
						}
					if (flag==1) 
					{
						String msg="Are you sure you want to create subscriber?\n" + "ID:"+s[1]+"\nFirst Name:"+s[2]+"\nLast Name:"+s[3]+"\nPhone Number:"+s[4]+"\nEmail:"+s[5];
						int answer=JOptionPane.showConfirmDialog(new JFrame(), msg, "New Subscriber",  JOptionPane.YES_NO_OPTION);
						if(answer==JOptionPane.YES_OPTION)
						MainGUI.OBLClient.handleMessageFromClient(s);
					}
			}
		});
		
		btnSubmit.setBounds(266, 271, 99, 31);
		add(btnSubmit);
		JLabel lblCreateNewReader = new JLabel("Create new Reader Card");
		lblCreateNewReader.setFont(new Font("Tahoma", Font.PLAIN, 29));
		lblCreateNewReader.setBounds(157, 13, 330, 49);
		add(lblCreateNewReader);
		remove(background);
		add(background);
		

	}
}
